package com.zqcy.workbench.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onemods.safekeyboard.SafeKeyboard;
import com.shinemo.minisdk.util.ToastUtil;
import com.zqcy.workbench.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * 输入交易密码弹窗
 */
public class TransferDialog extends Dialog {

    private SafeKeyboard safeKeyboard;


    private Context context;
    private TextView transferTitle;
    private TextView transferMoney;
    private TextView transferContinue;

    private EditText edt_code_input;
    private LinearLayout transfer_code_container;
    private TextView[] mTvInputs;
    private TextView tvCode1, tvCode2, tvCode3, tvCode4, tvCode5, tvCode6;

    public TransferDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        this.context = context;
        initView();
        initListener();
        Window window = getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams params = window.getAttributes();
        if (params != null) {
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.gravity = Gravity.CENTER;//设置dialog 在布局中的位置
        }
        window.setAttributes(params);
    }


    public void setTitle(String title) {
        transferTitle.setText(title);
    }

    @SuppressLint("SetTextI18n")
    public void setMoney(String money) {
        transferMoney.setText("￥" + money);
    }


    private void initView() {
        setContentView(R.layout.dialog_transfer);
        transferTitle = findViewById(R.id.transfer_title);
        transferMoney = findViewById(R.id.transfer_money);
        transferContinue = findViewById(R.id.transfer_continue);
        edt_code_input = findViewById(R.id.edt_code_input);
        transfer_code_container = findViewById(R.id.transfer_code_container);

        tvCode1 = findViewById(R.id.tv_code_1);
        tvCode2 = findViewById(R.id.tv_code_2);
        tvCode3 = findViewById(R.id.tv_code_3);
        tvCode4 = findViewById(R.id.tv_code_4);
        tvCode5 = findViewById(R.id.tv_code_5);
        tvCode6 = findViewById(R.id.tv_code_6);
        mTvInputs = new TextView[]{tvCode1, tvCode2, tvCode3, tvCode4, tvCode5, tvCode6};


    }

    private void changeContinue() {
        boolean isChange = true;
        for (int i = 0; i < 6; i++) {
            if (TextUtils.isEmpty(mTvInputs[i].getText().toString())) {
                isChange = false;
            }
        }
        if (isChange) {
            transferContinue.setEnabled(true);
            transferContinue.setBackgroundResource(R.drawable.shape_transfer_red_radius);
        } else {
            transferContinue.setEnabled(false);
            transferContinue.setBackgroundResource(R.drawable.shape_transfer_grey_radius);
        }
    }

    private void clearInput() {
        for (TextView textView : mTvInputs) {
            textView.setText("");
        }
    }

    private void initListener() {
        edt_code_input.addTextChangedListener(new MyTextWatcher() {
            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable editable) {
                clearInput();
                String s = editable.toString();
                for (int i = 0; i < s.length(); i++) {
                    mTvInputs[i].setText(s.charAt(i) + "");
                }
                changeContinue();
            }
        });
        transfer_code_container.setOnClickListener(view -> {
            edt_code_input.setFocusable(true);
            edt_code_input.setFocusableInTouchMode(true);
            edt_code_input.requestFocus();
            Utils.showSoftKeyBoard((Activity) context, edt_code_input);
        });
        transferContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = edt_code_input.getText().toString(); // 交易密码
                ToastUtil.show(context, "转账成功 ");
                dismiss();
            }
        });

    }


    @Override
    public void onBackPressed() {
        if (safeKeyboard.stillNeedOptManually(false)) {
            safeKeyboard.hideKeyboard();
            return;
        }
        super.onBackPressed();
    }


    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        if (safeKeyboard != null) {
            safeKeyboard.release();
            safeKeyboard = null;
        }
        super.setOnDismissListener(listener);
    }
}
